package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.queryobject.QueryObject;
import cn.wolfcode.resultpage.ResultPage;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    // mapper接口实现类对象
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public int delete(Long id) {
        // 调用DAO层接口方法
        return departmentMapper.delete(id);
    }

    @Override
    public int insert(Department department) {
        // 调用DAO层接口方法
        return departmentMapper.insert(department);
    }

    @Override
    public Department selectOne(Long id) {
        // 调用DAO层接口方法
        return departmentMapper.selectOne(id);
    }

    @Override
    public List<Department> listAll() {
        // 调用DAO层接口方法
        return departmentMapper.listAll();
    }

    @Override
    public int update(Department department) {
        // 调用DAO层接口方法
        return departmentMapper.update(department);
    }

   /* // service接口查询方法query
    public ResultPage<Department> query(QueryObject qo) {
        // 调用DAO层接口方法,查询数据总条数
        int totalCount = departmentMapper.queryForCount(qo);
        if (totalCount == 0){
            // ResultPage.emtry(qo.getPageSize());
            // 空集合,不是null,是这个集合长度为0, 里面没有元素
            return new ResultPage<>(qo.getCurrentPage(),qo.getPageSize(),totalCount, Collections.emptyList());
        }
         // 控制当前页的范围
        int totalPage = totalCount % qo.getPageSize() == 0 ?  totalCount / qo.getPageSize() : totalCount / qo.getPageSize() + 1;

        if (qo.getCurrentPage() > totalPage){
            qo.setCurrentPage(totalPage);
        }

        // 调用DAO层接口方法,查询当前页面的结果集数据
        List<Department> listData = departmentMapper.queryForList(qo);
        return new ResultPage<>(qo.getCurrentPage(),qo.getPageSize(),totalCount,listData);
    }*/

    public PageInfo<Department> query(QueryObject qo) {
        // 使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());// 当前页,每页显示数量,提供给插件计算
        List<Department> dataList = departmentMapper.queryForList(qo);// 里面的sql不需要写LIMIT
        return new PageInfo(dataList);
    }
}
